package com.ruoyi.common.core.flashpay.utils;


import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.IOUtils;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class FMHtmlUtils {

    public static String generatorHtml(String loadPath,Map<String,Object> data,String resFilePath,String tempFile) throws IOException {
        Configuration configuration = new Configuration();
        //模板加载路径
        configuration.setDirectoryForTemplateLoading(new File(loadPath));
        //获取模板对象
        Template template = configuration.getTemplate(tempFile);
        //模板和数据结合生成html字符串
        try {
            String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, data);
            //通过工具类转为输入流
            InputStream inputStream = IOUtils.toInputStream(html);
            resFilePath+=data.get("couponId")+".html";
            FileOutputStream fileOutputStream = new FileOutputStream(resFilePath);
            //输入流复制到输出流中
            IOUtils.copy(inputStream, fileOutputStream);
            return resFilePath;
        } catch (TemplateException e) {
            throw new RuntimeException("模板生成异常!");
        }
    }
}
